package pulpfizz.physics.contact;

import org.jbox2d.dynamics.ContactListener;
import org.jbox2d.dynamics.contacts.ContactResult;

import pulpfizz.physics.NewWorld;


/**
 * Stores contact points away for inspection, as with CCD
 * contacts may be gone by the end of a step.
 * 
 */
public class ConcreteContactListener implements ContactListener {
	private NewWorld newWorld;
	
	public ConcreteContactListener(NewWorld physicsHandler) {
		this.newWorld = physicsHandler;
	}
	
	public void add(org.jbox2d.dynamics.contacts.ContactPoint point) {
		if (this.newWorld.getContactPointCount() == NewWorld.k_maxContactPoints) {
			return;
		}

		ContactData cp = this.newWorld.getContactPoints()[this.newWorld.getContactPointCount()];
		cp.shape1 = point.shape1;
		cp.shape2 = point.shape2;
		cp.position = point.position.clone();
		cp.normal = point.normal.clone();
		cp.friction = point.friction;
		cp.id = point.id;
		cp.restitution = point.restitution;
		cp.separation = point.separation;
		cp.velocity = point.velocity.clone();
		cp.state = 0;

		this.newWorld.setContactPointCount(this.newWorld.getContactPointCount() + 1);	
	}
	public void persist(org.jbox2d.dynamics.contacts.ContactPoint point) {
		if (this.newWorld.getContactPointCount() == NewWorld.k_maxContactPoints) {
			return;
		}

		ContactData cp = this.newWorld.getContactPoints()[this.newWorld.getContactPointCount()];
		cp.shape1 = point.shape1;
		cp.shape2 = point.shape2;
		cp.position = point.position.clone();
		cp.normal = point.normal.clone();
		cp.friction = point.friction;
		cp.id = point.id;
		cp.restitution = point.restitution;
		cp.separation = point.separation;
		cp.velocity = point.velocity.clone();
		cp.state = 1;

		this.newWorld.setContactPointCount(this.newWorld.getContactPointCount() + 1);
	}
	public void remove(org.jbox2d.dynamics.contacts.ContactPoint point) {
		if (this.newWorld.getContactPointCount() == NewWorld.k_maxContactPoints) {
			return;
		}

		ContactData cp = this.newWorld.getContactPoints()[this.newWorld.getContactPointCount()];
		cp.shape1 = point.shape1;
		cp.shape2 = point.shape2;
		cp.position = point.position.clone();
		cp.normal = point.normal.clone();
		cp.friction = point.friction;
		cp.id = point.id;
		cp.restitution = point.restitution;
		cp.separation = point.separation;
		cp.velocity = point.velocity.clone();
		cp.state = 2;

		this.newWorld.setContactPointCount(this.newWorld.getContactPointCount() + 1);
	}
	
	public void result(ContactResult point) {
		//do nothing for now
	}
}